package fw.qaz.asmx.reader.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;
import java.util.List;

import fw.qaz.asmx.reader.R;
import fw.qaz.asmx.reader.utils.Convertor;

public class FileListAdapter extends RecyclerView.Adapter<FileListAdapter.FileViewHolder>
{
    private List<File> lst;

    private Drawable dDir;
    private Drawable dTxt;

    private RecyclerViewItemClickListener clickListener;
    private RecyclerViewItemLongClickListener longClickListener;

    public FileListAdapter(@NonNull Context cxt, @NonNull List<File> lst)
    {
        this.lst = lst;

        dDir = ResourcesCompat.getDrawable(cxt.getResources(), R.mipmap.folder, null);
        dTxt = ResourcesCompat.getDrawable(cxt.getResources(), R.mipmap.txt_file, null);
    }

    @Override public FileViewHolder onCreateViewHolder(ViewGroup parent, int pos)
    {
        View v = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.list_file_browse_item, parent, false);
        return new FileViewHolder(v);
    }

    @Override public void onBindViewHolder(FileViewHolder vh, int pos)
    {
        File f = lst.get(pos);
        if (f.isDirectory())
        {
            vh.iv.setImageDrawable(dDir);
            vh.tvSize.setText(R.string.folder);
        }
        else
        {
            vh.iv.setImageDrawable(dTxt);
            vh.tvSize.setText(Convertor.individuatedFileSize((int) f.length()));
        }
        vh.tvName.setText(f.getName());
    }

    @Override public int getItemCount()
    {
        return lst.size();
    }

    public class FileViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener, View.OnLongClickListener
    {
        public ImageView iv;
        public TextView tvName;
        public TextView tvSize;

        public FileViewHolder(View v)
        {
            super(v);
            iv = (ImageView) v.findViewById(R.id.iv_file_type);
            tvName = (TextView) v.findViewById(R.id.tv_file_name);
            tvSize = (TextView) v.findViewById(R.id.tv_file_size);
            v.setOnClickListener(this);
            v.setOnLongClickListener(this);
        }

        @Override public void onClick(View v)
        {
            if (clickListener != null)
            {
                clickListener.onItemClick(v, getAdapterPosition());
            }
        }

        @Override public boolean onLongClick(View v)
        {
            if (longClickListener != null)
            {
                longClickListener.onItemLongClick(v, getAdapterPosition());
            }
            return true;
        }
    }

    public void setOnClickListener(RecyclerViewItemClickListener listener)
    {
        clickListener = listener;
    }

    public void setOnItemLongClickListener(RecyclerViewItemLongClickListener listener)
    {
        longClickListener = listener;
    }

    public interface RecyclerViewItemClickListener
    {
        void onItemClick(View v, int pos);
    }

    public interface RecyclerViewItemLongClickListener
    {
        void onItemLongClick(View v, int pos);
    }
}
